package com.explorati.LeetCode001.twosum1;

import java.util.HashMap;
import java.util.Map;

/**
 * 1. Two Sum
 * 
 * Given an array of integers, return indices of the two numbers such that they add up to a specific target.

	You may assume that each input would have exactly one solution, and you may not use the same element twice.
 * @author explorati
 *
 */
public class Solution {
	public int[] twoSum(int[] nums, int target) {
		int[] arr = new int[2];
		Map<Integer, Integer> map = new HashMap<Integer, Integer>(8);
		for (int i = 0; i < nums.length; i++) {
			int num = target - nums[i];
			if (map.containsKey(num)) {
				arr[0] = map.get(num);
				arr[1] = i;
				return arr;
			} else {
				map.put(nums[i], i);
			}
		}
		return arr;
	}
}
